﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
//using PolePosition.v3.EntityLibrary;

namespace PolePosition.v3.Desktop
{
    public partial class FormMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FormMain()
        {
            InitializeComponent();

            // reference in program
            //Program.FormMain = this;

            Visible = false;
            //hide all tabs
            tabItemHome.Visible = false;
            tabItemMap.Visible = false;
            tabItemData.Visible = false;
            tabItemOther.Visible = false;
            tabItemReports.Visible = false;
            tabItemWorkQueue.Visible = false;
            tabItemDirections.Visible = false;

            //start load of lookup tables immediately
            //backgroundWorkerLoadLookupTables.RunWorkerAsync();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //FormLogin _Login = new FormLogin();
            //if (_Login.ShowDialog() != DialogResult.OK)
            //{
            //    if (backgroundWorkerLoadLookupTables.IsBusy)
            //        backgroundWorkerLoadLookupTables.CancelAsync();
            //    Application.Exit();
            //    return;
            //}

            ////set the appropriate tab to visible
            //switch (Program.LoginResult.LoginUserType)
            //{
            //    case LoginEntity.LoginUserTypes.Vendor:
            //        ribbonTabItemAdmin.Visible = false;
            //        ribbonTabItemUtilityHome.Visible = false;
            //        ribbonTabItemVendorHome.Visible = true;
            //        ribbonTabItemVendorHome.Select();
            //        break;
            //    case LoginEntity.LoginUserTypes.Utility:
            //        ribbonTabItemAdmin.Visible = false;
            //        ribbonTabItemUtilityHome.Visible = true;
            //        ribbonTabItemVendorHome.Visible = false;
            //        ribbonTabItemUtilityHome.Select();

            //        //a little customization - put utility name in the ribbon group
            //        if (Program.LoginResult.Utility != null && !string.IsNullOrEmpty(Program.LoginResult.Utility.Name))
            //            ribbonBarUtilityProjects.Text = Program.LoginResult.Utility.Name;

            //        Program.UserSelections.UtilityId = Program.LoginResult.Utility.UtilityId.Value;

            //        break;
            //    case LoginEntity.LoginUserTypes.PPI:

            //        if (Program.LoginResult.Utilities == null ||
            //            Program.LoginResult.Utilities.Count() != 1)
            //        {
            //            buttonItemUtilityProjectViewSelectUtility.Visible = true;
            //            buttonItemUtilityProjectViewInGrid.Enabled = false;
            //            buttonItemUtilityProjectViewReports.Enabled = false;
            //            buttonItemUtilityProjectViewWorkQueue.Enabled = false;
            //            buttonItemUtiltyProjectViewInMap.Enabled = false;
            //        }
            //        else
            //        {
            //            //only one utility
            //            buttonItemUtilityProjectViewSelectUtility.Visible = false;
            //            Program.UserSelections.UtilityId = Program.LoginResult.Utilities[0].UtilityId.Value;
            //        }

            //        ribbonTabItemAdmin.Visible = true;
            //        ribbonTabItemUtilityHome.Visible = true;
            //        ribbonTabItemVendorHome.Visible = true;
            //        ribbonTabItemAdmin.Select();
            //        break;
            //    case PolePosition.v3.EntityLibrary.LoginEntity.LoginUserTypes.Unknown:
            //    default:
            //        //really cannot happen if we get to here
            //        break;
            //}

            //Visible = true;
        }

        private void ribbonTabItemVendorHome_Click(object sender, EventArgs e)
        {

        }

        private void buttonItem14_Click(object sender, EventArgs e)
        {
            //POTENTIALLY LOAD A GRID



        }

        //public Utility.TabMap.MapDisplay MapControl
        //{
        //    get
        //    {
        //        return tabMap1.Map;
        //    }
        //}

        //public void ShowMapTab()
        //{
        //    return;
        //    tabItemMap.Visible = true;
        //    tabControlMain.SelectedTab = tabItemMap;
        //}

        //private void backgroundWorkerLoadLookupTables_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    e.Result = ServiceProxy.AdministrationService.GetLookups();

        //    if ((sender as BackgroundWorker).CancellationPending)
        //        e.Cancel = true;
        //}

        //private void backgroundWorkerLoadLookupTables_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    if (e.Cancelled)
        //        return;

        //    if (e.Error != null)
        //    {
        //        //not really sure what to do here - for now show a messagebox and close the app
        //        DevComponents.DotNetBar.MessageBoxEx.Show("A Critical Error has occurred attempting to load the application. Application will close. (ref:lookup)", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        Application.Exit();
        //        return;
        //    }

        //    Program.LookupTableData = e.Result as EntityLibrary.LookupTables;
        //}

        //#region Utility Ribbon Buttons

        //private void buttonItemUtiltyProjectViewInMap_Click(object sender, EventArgs e)
        //{
        //    var _MapElements = new Selectors.SelectMapElements();
        //    if (_MapElements.ShowDialog() == DialogResult.OK)
        //    {
        //        //load map
        //        ShowMapTab();
        //        MapControl.LoadKmlFromString(PolePosition.v3.Common.Enumerations.DataTypes.Custom, _MapElements.Kml);
        //    }
        //}

        //private void buttonItemUtiltyProjectViewInMap2_Click(object sender, EventArgs e)
        //{
        //    string kml = string.Empty;

        //    switch (Program.LoginResult.LoginUserType)
        //    {
        //        case LoginEntity.LoginUserTypes.Utility:
        //            kml = ServiceProxy.UtilityService.GetUtilityKMLString(Program.LoginResult.Utility.UtilityId, Program.LoginResult.UtilityContact.Utility_ContactId);
        //        //case LoginEntity.LoginUserTypes.Vendor:
                   
        //        break;
        //    }

        //    //load map
        //    ShowMapTab();
        //    MapControl.LoadKmlFromString(Common.Enumerations.DataTypes.Custom, kml);
        //} 

        //private void buttonItemProjectData_Click(object sender, EventArgs e)
        //{
        //    string _ProjectName = null;
        //    int? _ProjectId = null;
        //    Common.Enumerations.DataTypes _ProjectType = Common.Enumerations.DataTypes.None;

        //    if (Program.LoginResult.Projects == null ||
        //       Program.LoginResult.Projects.Count() != 1)
        //    {
        //        Selectors.SelectDataElements _DataElements = new Selectors.SelectDataElements(Selectors.SelectDataElements.SelectDataElementTypes.Projects);
        //        if (_DataElements.ShowDialog() == DialogResult.OK)
        //        {
        //            _ProjectId = _DataElements.SelectedId;
        //            _ProjectName = _DataElements.SelectedName;
        //            _ProjectType = _DataElements.SelectedType;
        //        }
        //        _DataElements.Dispose();
        //    }
        //    else
        //    {
        //        _ProjectId = Program.LoginResult.Projects[0].Vendor_ProjectId;
        //        _ProjectName = Program.LoginResult.Projects[0].Name;

        //        if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionGpsVisit)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit;
        //        else if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionInspection)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsXmissionInspection;
        //        else if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.DistributionInspection)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;
        //    }

        //    if (!_ProjectId.HasValue || _ProjectType == Common.Enumerations.DataTypes.None)
        //        return;

        //    //load grid
        //    tabItemData.Visible = true;
        //    tabControlMain.SelectedTab = tabItemData;
        //    switch (_ProjectType)
        //    {
        //        case Common.Enumerations.DataTypes.ProjectsXmissionInspection:
        //            tabData1.ShowXmissionInspectionData(_ProjectId.Value, _ProjectName);
        //            break;
        //        case Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit:
        //            tabData1.ShowXmissionGPSData(_ProjectId.Value, _ProjectName);
        //            break;
        //        case Common.Enumerations.DataTypes.ProjectsDistributionInspection:
        //            tabData1.ShowDistributionInspectionData(_ProjectId.Value, _ProjectName);
        //            break;
        //        case Common.Enumerations.DataTypes.ProjectsDistributionGpsVisit:
        //            break;
        //        default:
        //            break;
        //    }
        //}

        //private void buttonItemAssetData_Click(object sender, EventArgs e)
        //{
        //    Selectors.SelectDataElements _DataElements = new Selectors.SelectDataElements(Selectors.SelectDataElements.SelectDataElementTypes.Assets);
        //    if (_DataElements.ShowDialog() == DialogResult.OK)
        //    {
        //        //load grid
        //        tabItemData.Visible = true;

        //        switch (_DataElements.SelectedType)
        //        {
        //            case Common.Enumerations.DataTypes.AssetsXmission:
        //                tabData1.ShowXmissionAssetData();
        //                break;
        //            case Common.Enumerations.DataTypes.AssetsDistribution:
        //                tabData1.ShowDistibutionAssetData();
        //                break;
        //            case Common.Enumerations.DataTypes.AssetsMeters:
        //                tabData1.ShowMeterAssetData();
        //                break;
        //            default:
        //                break;
        //        }
        //    }
        //}

        //private void buttonItemUtilityProjectViewReports_Click(object sender, EventArgs e)
        //{
        //    string _ProjectName = null;
        //    int? _ProjectId = null;
        //    Common.Enumerations.DataTypes _ProjectType = Common.Enumerations.DataTypes.None;

        //    if (Program.LoginResult.Projects == null ||
        //       Program.LoginResult.Projects.Count() != 1)
        //    {
        //        Selectors.SelectDataElements _DataElements = new Selectors.SelectDataElements(Selectors.SelectDataElements.SelectDataElementTypes.Projects);
        //        if (_DataElements.ShowDialog() == DialogResult.OK)
        //        {
        //            _ProjectId = _DataElements.SelectedId;
        //            _ProjectName = _DataElements.SelectedName;
        //            _ProjectType = _DataElements.SelectedType;
        //        }
        //        _DataElements.Dispose();
        //    }
        //    else
        //    {
        //        _ProjectId = Program.LoginResult.Projects[0].Vendor_ProjectId;
        //        _ProjectName = Program.LoginResult.Projects[0].Name;

        //        if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionGpsVisit)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit;
        //        else if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.XmissionInspection)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsXmissionInspection;
        //        else if (Program.LoginResult.Projects[0].ProjectTypeFk.Value == Common.Constants.ProjectTypes.DistributionInspection)
        //            _ProjectType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;
        //    }

        //    if (!_ProjectId.HasValue || _ProjectType == Common.Enumerations.DataTypes.None)
        //        return;

        //    //load reports tab
        //    tabItemReports.Visible = true;
        //    tabControlMain.SelectedTab = tabItemReports;
        //    tabReports1.ShowReports(Program.UserSelections.UtilityId, _ProjectId.Value, _ProjectName, _ProjectType);
        //}

        //private void buttonItemUtilityProjectViewWorkQueue_Click(object sender, EventArgs e)
        //{
        //    tabItemWorkQueue.Visible = true;
        //    tabControlMain.SelectedTab = tabItemWorkQueue;
        //    tabWorkQueue1.ShowWorkQueueData();
        //}

        //#endregion

        //#region Vendor Ribbon Buttons

        //#region Project Ribbon Group

        //private void buttonItemVendorProjectViewLoad_Click(object sender, EventArgs e)
        //{
        //    Selectors.SelectProject _Project = new Selectors.SelectProject();
        //    if (_Project.ShowDialog() == DialogResult.OK)
        //    {
        //        if (Program.UserSelections.VendorSelectedProject == null)
        //        {
        //            ribbonBarVendorProjects.Text = "Project";
        //            buttonItemVendorProjectViewMap.Enabled = false;
        //            buttonItemVendorProjectViewData.Enabled = false;
        //            buttonItemVendorProjectViewReports.Enabled = false;
        //            buttonItemVendorMobileDeploy.Enabled = false;
        //            buttonItemVendorMobilePublish.Enabled = false;
        //            buttonItemVendorMobileRetrieve.Enabled = false;
        //        }
        //        else
        //        {
        //            ribbonBarVendorProjects.Text = Program.UserSelections.VendorSelectedProject.Name;
        //            buttonItemVendorProjectViewMap.Enabled = true;
        //            buttonItemVendorProjectViewData.Enabled = true;
        //            buttonItemVendorProjectViewReports.Enabled = true;
        //            buttonItemVendorMobileDeploy.Enabled = true;
        //            buttonItemVendorMobilePublish.Enabled = true;
        //            buttonItemVendorMobileRetrieve.Enabled = true;
        //        }
        //    }
        //}

        //private void buttonItemVendorProjectViewMap_Click(object sender, EventArgs e)
        //{

        //}

        //private void buttonItemVendorProjectViewData_Click(object sender, EventArgs e)
        //{
        //    //
        //}

        //private void buttonItemVendorProjectViewReports_Click(object sender, EventArgs e)
        //{
        //    //
        //}

        //#endregion

        //#region Mobile Ribbon Group
        
        //private void buttonItemVendorMobilePublish_Click(object sender, EventArgs e)
        //{
        //    //
        //}

        //private void buttonItemVendorMobileDeploy_Click(object sender, EventArgs e)
        //{
        //    //
        //}

        //private void buttonItemVendorMobileRetrieve_Click(object sender, EventArgs e)
        //{
        //    //
        //}

        //#endregion

       
        //#endregion

        //private void buttonXTestKml_Click(object sender, EventArgs e)
        //{
        //    string _Kml;
        //    _Kml = ServiceProxy.UtilityService.TestKml();
        //    MapControl.LoadKmlFromString(Common.Enumerations.DataTypes.Custom, _Kml);
        //}

        //private void buttonItemUtilityProjectViewSelectUtility_Click(object sender, EventArgs e)
        //{
        //    Selectors.SelectUtility _SelectUtility = new Selectors.SelectUtility();
        //    _SelectUtility.ShowDialog();
        //    _SelectUtility.Dispose();

        //    if (Program.UserSelections.UtilityId == -1)
        //    {
        //        buttonItemUtilityProjectViewInGrid.Enabled = false;
        //        buttonItemUtilityProjectViewReports.Enabled = false;
        //        buttonItemUtilityProjectViewWorkQueue.Enabled = false;
        //        buttonItemUtiltyProjectViewInMap.Enabled = false;
        //        ribbonBarUtilityProjects.Text = "Utility";
        //    }
        //    else
        //    {
        //        buttonItemUtilityProjectViewInGrid.Enabled = true;
        //        buttonItemUtilityProjectViewReports.Enabled = true;
        //        buttonItemUtilityProjectViewWorkQueue.Enabled = true;
        //        buttonItemUtiltyProjectViewInMap.Enabled = true;

        //        var _DefaultUtility = from utility in Program.LoginResult.Utilities
        //                              where utility.UtilityId == Program.UserSelections.UtilityId
        //                              select utility;

        //        if (_DefaultUtility == null || _DefaultUtility.Count() == 0)
        //            ribbonBarUtilityProjects.Text = "Utility";
        //        else
        //            ribbonBarUtilityProjects.Text = _DefaultUtility.First().Name;
        //    }
        //}

        //public void ShowDirections(string title, string url)
        //{
        //    if (string.IsNullOrEmpty(title))
        //        title = "Directions";

        //    tabItemDirections.Visible = true;
        //    tabControlMain.SelectedTab = tabItemDirections;
        //    tabItemDirections.Text = title;
        //    tabBrowser1.ShowBrowser(title, url);
        //}

        //private void tabControlMain_TabItemClose(object sender, DevComponents.DotNetBar.TabStripActionEventArgs e)
        //{
        //    e.Cancel = true;
        //    tabControlMain.SelectedTab.Visible = false;
        //}

        //private void buttonItemHelp_Click(object sender, EventArgs e)
        //{
        //    tabItemDirections.Visible = true;
        //    tabControlMain.SelectedTab = tabItemDirections;
        //    tabItemDirections.Text = "Pole Position Help";
        //    tabBrowser1.ShowBrowser("Pole Position Help", "http://dev-poleposition.com/PolePosition/Help/default.htm");
        //}

    
    }
}
